const Koa = require('koa');
const Router = require('koa-router');
const glob = require("glob");
const logger = require('koa-logger')
const { resolve } = require('path');
const fs = require('fs');
 
const app = new Koa();
const router = new Router({prefix: '/api'});
const routerMap = {};  // 存放路由映射

// 引入插件
const cors = require('koa2-cors');
// 配置插件
app.use(cors({
    // 任何地址都可以访问
    origin:"*",
    // 指定地址才可以访问
    // origin: 'http://localhost:8080',
    maxAge: 2592000,
    // 必要配置
    credentials: true
}));

app.use(logger());

// 注册路由
glob.sync(resolve('./api', "**/*.json")).forEach((item, i) => {
    let apiJsonPath = item && item.split('/api')[1];
    // let apiPath = apiJsonPath.replace('.json', '');
    let apiPath = apiJsonPath;

    router.get(apiPath, (ctx, next) => {
        try {
            let jsonStr = fs.readFileSync(item).toString();
            ctx.body = {
                ...JSON.parse(jsonStr),
                // data: JSON.parse(jsonStr),
                // state: 200,
                // type: 'success' // 自定义响应体
            }
        }catch(err) {
            ctx.throw('服务器错误', 500);
        }
      });
    
    // 记录路由
    routerMap[apiJsonPath] = apiPath;
});

fs.writeFile('./routerMap.json', JSON.stringify(routerMap, null , 4), err => {
    if(!err) {
        console.log('路由地图生成成功！')
    }
});
 
app
  .use(router.routes())
  .use(router.allowedMethods());

app.listen(9000);
